#!/bin/bash
#Author: YangYi
#Date: 2022-09-08
#Version: v1

#UpDate: 2022-10-26
#Version: v2 
#Content: Add FileCleanup 


#UpDate: 2023-11-24
#Version: v3 
#Content: Add



#脚本运行时间统计
#tartTime=`date +%Y%m%d-%H:%M:%S`
#startTime_s=`date +%s`


#告警伐值
ALERT_VALUE="100000"

#设置要保存的文件个数
SAVE_COUNT="48"

#需要统计的服务个数(按照最高排名降序)
RANKING_VALUE="1"

#接受者邮箱
#EMAIL_RECIVER="973534823@qq.com"
EMAIL_RECIVER="oyhh71087475.sh@chinatelecom.cn jinjiaxin@tontron.com.cn huangjm17@chinatelecom.cn xiexp2@chinatelecom.cn tongfy@chinatelecom.cn yangyi@tontron.com.cn zhangjie3@ideal.sh.cn yutao1@ideal.sh.cn wangpeng1@ideal.sh.cn tangcy@ideal.sh.cn  guodc@ideal.sh.cn zhusj7@chinatelecom.cn guodechao.sh@chinatelecom.cn zhangj119@chinatelecom.cn"

#接受者短信
#PAAS_PHONE=["13564881037"]
PAAS_PHONE=["18939860223","18338822990","13227009978","17783795215","17758677587","19916970183","13564881037","17635570615","13513842121","18800295579","18317519326","17321261159","13381729139","19916970139"]


#格式化时间
TIME=`date +%Y:%m:%d:%H:%M:%S`

#节点容器统计
#DOCKER_SERVICES=`sudo docker ps | grep -v "CONTAINER\|pause\|traefik" | awk '{print $1}'`

#节点进程统计
NODE_PID=`sudo ps aux | awk '{print $2}' | grep -v PID`

#节点进程统计(容器)
#SERVICES_PID=`for i in $DOCKER_SERVICES; do sudo docker inspect -f '{{.State.Pid}}' $i; done`

#存储路径
FILE_PATH=/app/paasadmin/services_pid/$TIME-statistics.txt

#$临时文件
TEMPORARY_FILE=/app/paasadmin/TemporaryFiles.txt

#文件过滤路径
FILTER_FILE=/app/paasadmin/services_pid/
/bin/mkdir -p  $FILTER_FILE && chown -R paasadmin:paasadmin $FILTER_FILE

#节点IP
NODE_IP=`/sbin/ip a s  | grep -iw "eth0\|bond0" | grep -i "10.145." | awk '{print $2}' | awk -F "/" '{print $1}' | head -1`


#句柄数服务统计
function Open_File_Statistics(){
  if [  -f $TEMPORARY_FILE ];then
    rm -f $TEMPORARY_FILE
  fi
  for pid in $NODE_PID
    do
      sudo  ls -l /proc/$pid/fd | wc -l | awk '{printf $i":"}' >> $TEMPORARY_FILE && echo -ne "$pid"":" >> $TEMPORARY_FILE && sudo ps aux | awk '{if($2=='$pid'){print }}' | grep -o "dockerId.*" | awk '{print $1}' | awk -F "=" '{printf $2}' >> $TEMPORARY_FILE && echo -e ":"$NODE_IP"\c" >> $TEMPORARY_FILE && echo -e ":"$TIME"\c" >> $TEMPORARY_FILE &&  echo -e "\n" >> $TEMPORARY_FILE
    done
  sudo cat $TEMPORARY_FILE | sort  -nr | head -n $RANKING_VALUE  > $FILE_PATH 
  
}


#文件内容测试
function Filter_FILE(){ 
  FILTER_FILE_1=`sudo ls -l $FILTER_FILE | tail -n1 | awk '{print $9}'`
  #FILTER_FILE_2=`ls -l $FILTER_FILE | tail -n2 | awk '{print $9}' | head -n1`
  sudo cat $FILTER_FILE$FILTER_FILE_1 #&& sleep 2 && cat $FILTER_FILE$FILTER_FILE_2  
}


#文件清理
function File_Cleanup(){

cd $FILTER_FILE
#1 判断文件个数
count=$(ls -l | wc -l)
delcount=$[$count - $SAVE_COUNT]
#2 是否进行删除
if [ $delcount -gt 0 ] ; then
        #获取时间最久的文件夹名称
        filesname=$(ls -tr | head -n $delcount)
        filesname=${filesname// /}
        filesarr=($filesname)
        #循环依次删除时间久的文件
        for each in ${filesarr[*]}
          do
          sudo rm -rf $each
          done
fi
}


#邮件服务
function Mail_Server(){
  #邮件内容
  FILTER_FILE_1=`sudo ls -l $FILTER_FILE | tail -n1 | awk '{print $9}'`
  Content_Of_Email=`sudo cat $FILTER_FILE$FILTER_FILE_1 | awk -F ":" '{if($3==""){print "\"""告警:(句柄数:"$1"),(PID:"$2"),(系统进程(非容器服务))""\""} else {print "\"""告警:(句柄数:"$1"),(PID:"$2"),(服务名:"$3")""\""}}'`
  #发送者邮箱
  email_sender="g-paas_monitor.sh@chinatelecom.cn"
  #邮箱用户名
  email_username="g-paas_monitor.sh@chinatelecom.cn"
  #邮箱密码
  #使用qq邮箱进行发送需要注意：首先需要开启：POP3/SMTP服务，其次发送邮件的密码需要使用在开启POP3/SMTP服务时候腾讯提供的第三方客户端登陆码。
  email_password="3xT)zQKUf!L5vzKN"
  #email_password=Jiekou@12
  #smtp服务器地址
  email_smtphost=smtp.chinatelecom.cn
  email_title="节点: $NODE_IP 句柄数告警"
  email_content="$Content_Of_Email"

  #取消SSl -o tls=no     使用sendEmail-v1.56需要使用5.10版本的Perl才能够成功发送邮件
  sudo /usr/local/bin/sendEmail  -f ${email_sender} -t ${EMAIL_RECIVER} -s ${email_smtphost} -u ${email_title} -xu ${email_username} -xp ${email_password} -m ${email_content} -a ${file1_path} ${file2_path} -o tls=no -o message-charset=utf-8
}


#伐值告警
function Alert(){
  FILTER_FILE_1=`sudo ls -l $FILTER_FILE | tail -n1 | awk '{print $9}'`
  HANDLES=`sudo cat $FILTER_FILE$FILTER_FILE_1 | awk 'NR==1' | awk -F : '{print $1}'`  
  if [ $HANDLES -ge $ALERT_VALUE ]; then
    Mail_Server
    sleep 1
    Sms_Server
  fi
}

function Sms_Server(){
  #短信内容
  FILTER_FILE_1=`sudo ls -l $FILTER_FILE | tail -n1 | awk '{print $9}'`
  IP=`echo $NODE_IP | awk -F "." '{printf $1"."$2"."$3"."$4}'`
  #echo $IP
  Content_Of_Sms=`sudo sed -i 's/\n//g' $FILTER_FILE$FILTER_FILE_1 | cat $FILTER_FILE$FILTER_FILE_1 | awk -F ":" '{if($3==""){print "\"""'$IP'""-告警:(句柄数:"$1"),(PID:"$2"),(系统进程(非容器服务))""\""} else {printf "\"""'$IP'""-告警:(句柄数:"$1"),(PID:"$2"),(服务名:"$3")""\""}}'`
  #echo $Content_Of_Sms
  #curl http://10.145.167.219:8070/plat/shtelpaassms/send  -X POST -H "Content-type:application/json" -d '{"phone":'$PAAS_PHONE',"msg":'$Content_Of_Sms',"userName":"bdbc","pwd":"d061A1wV"}'
  curl http://10.145.167.219:8070/plat/shtelpaassms/send  -X POST -H "Content-type:application/json" -d '{"phone":'$PAAS_PHONE',"msg":'$Content_Of_Sms',"userName":"test","pwd":"123456"}'
  #curl http://10.145.167.219:8070/plat/shtelpaassms/send  -X POST -H "Content-type:application/json" -d '{"phone":'$PAAS_PHONE',"msg":"内容|.测试","userName":"test","pwd":"123456"}'
}


Open_File_Statistics
#Filter_FILE
File_Cleanup
#Mail_Server
Alert
#Sms_Server


#endTime=`date +%Y%m%d-%H:%M:%S`
#endTime_s=`date +%s`
#sumTime=$[ $endTime_s - $startTime_s ]
#echo "$startTime ---> $endTime" "Total:$sumTime seconds"







